package com.runa.rccprotocol.analysis.En13757.decoder;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author diandian
 * @Date 2022/1/7 13:44
 */
public class Dif_Function_Datef implements Dif_Function {
    private DateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");

    @Override
    public Date handle(byte[] bytes, int start, int length) {
        int minute = bytes[start] & 0x7f;
        int hour = bytes[start + 1] & 0x3f;
        int day = bytes[start + 2] & 0x3f;
        int month = bytes[start + 3] & 0xf;
        int year = ((bytes[start + 3] & 0xf0) >> 1) | ((bytes[start + 2] & 0xe0) >> 5);
        try {
            return format.parse(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":00");
        } catch (ParseException e) {
            
        }
        return null;
    }
}
